package com.cqhilink.iot.jobstandardmanagement.services.innerservice.button.util;

import com.cqhilink.iot.jobstandardmanagement.common.bean.ApplicationContextHelper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.MenuButton;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasMenuFunc;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmMenuButtonService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class MenuButtonPool implements Runnable {
    private List<UasMenuFunc> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setList(List<UasMenuFunc> list) {
        this.list = list;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    @Override
    public void run() {
        JsmMenuButtonService menuButtonService = (JsmMenuButtonService) ApplicationContextHelper.getBean("menuButtonService");
        if (menuButtonService != null && StringUtils.isNotEmpty(controlType)) {
            List<MenuButton> paramList = new ArrayList<MenuButton>();
            try {
                menuButton(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//处理菜单按钮关联信息
                if ("1".equals(controlType)) {
                    //添加
                    menuButtonService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    menuButtonService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    menuButtonService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 处理菜单按钮关联信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void menuButton(List<UasMenuFunc> list, List<MenuButton> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            for (UasMenuFunc umf : list) {
                MenuButton param = new MenuButton();
                BeanUtils.copyProperties(param, umf);
                param.setModifyer("");
                param.setButtonId(umf.getFuncId());
                Date date = new Date();
                param.setCreatetime(date);
                param.setModifytime(date);
                param.setCreator("");
                paramList.add(param);
            }
        }
    }

}
